<?
$smarty=new Smarty();
if(!ANTIHACK) die("ANTIHACK...");
$smarty->assign("nameact","Bảng phân quyền");
if(isset($_GET['do']) && $_GET['do']=="del" && intval($_GET['id'])>0 )
{
	$query=query("delete from `group` where id=".intval($_GET['id']));
	if($query)
		header("location:".base_admin."?act=group");
	else
		$smarty->assign("msg",$msg["error"]);
}
if($_POST['save'])
{
	$data = array(
		"name"=>addslashes($_POST['name']),
	);
	$per=array($_POST['ht'],"mn"=>$_POST['mn']);
	$enper=json_encode($per);
	$data['permission']=$enper;
	if(intval($_POST[id])>0)
	{
		$ret=update($data,"`group`","id=".intval($_POST['id']));
		if($ret)
			$smarty->assign("msg","Update nhóm thành công<script>setTimeout('location.href=\"".base_admin."?act=group\"',1000);</script>");
		else
			$smarty->assign("msg","Có lỗi xảy ra");
	}
	else
	{
		$ret=insert($data,"`group`");
		if($ret)
			$smarty->assign("msg","Thêm nhóm thành công<script>setTimeout('location.href=\"".base_admin."?act=group\"',1000);</script>");
		else
			$smarty->assign("msg","Có lỗi xảy ra");
	}
}
	if($_GET[id] && intval($_GET[id])>0)
	{
		$id=intval($_GET['id']);
		$getper=query("select id,name,permission from `group` where id=".$id);
		$fgp=fetch($getper);
		$listper=json_decode($fgp['permission'],true);
		$smarty->assign("data",$fgp);
	}
	$mn=query("select * from admin_menu where parent=0");
	$i=0;
	while($rmn=fetch($mn))
	{
		$i++;
		$it=query("select * from admin_menu where parent=".$rmn[id]);
		while($rit=fetch($it))
		{
			$rit['parentid']=$rmn['id'];
			if($listper[0][$rmn['id']][$rit['id']])
				$rit['ischeck']='checked="checked"';
			$menu[$rmn['name']][]=$rit;
		}
		if($i%2==0)
			$menu[$rmn['name']]['end']=1;
		else
			$menu[$rmn['name']]['end']=2;
	}
	$bx=query("select * from menu");
	while($rbx=fetch($bx))
	{
		if($listper['mn'][$rbx['id']])
			$rbx['ischeck']='checked="checked"';
		if($rbx['type']=="page")
			$page[]=$rbx;
		else
			$box[]=$rbx;
	}
$smarty->assign("menu",$menu);
$smarty->assign("page",$page);
$smarty->assign("box",$box);
$smarty->display("edit-group.htm");
?>